package shogi;

/**
 *
 * @author Charles Ritchea
 * @author Cody Kraatz
 */
public class ShogiController {
        private int[] leftPlacement;
        private int[] rightPlacement;
	private boolean legalBishop;

	public ShogiController() {
		leftPlacement = new int[81];
		rightPlacement = new int[81];
		legalBishop = false;
	}

	public boolean isStopBishop() {
		return legalBishop;
	}

	public void setStopBishop(boolean stopBishop) {
		this.legalBishop = stopBishop;
	}

	public int getLeftPlacement(int i) {
		return leftPlacement[i];
	}

	public void setLeftPlacement(int i, int value) {
		this.leftPlacement[i] = value;
	}

	public int getRightPlacement(int i) {
		return rightPlacement[i];
	}

	public void setRightPlacement(int i, int value) {
		this.rightPlacement[i] = value;
	}

	public boolean isLegal(int originalIndex, int destinationIndex, ShogiPiece aShogiPiece){
		int originalRow = originalIndex / 9;
		int originalColumn = originalIndex % 9;
		int destinationRow = destinationIndex / 9;
		int destinationColumn = destinationIndex % 9;
		switch(aShogiPiece.getType()){
			case 'n':
				if((destinationRow == (originalRow - 1) || destinationRow == (originalRow + 1))){
					if(aShogiPiece.getOrientation().equalsIgnoreCase("left")){
						if(destinationColumn == (originalColumn + 2))
							return true;
						else
							return false;
						
					}
								
					else if(aShogiPiece.getOrientation().equalsIgnoreCase("right")){
						if(destinationColumn == (originalColumn - 2))
							return true;
						else
							return false;
					}
				}
					else
						return false;
			case 'g':
				if(originalRow == destinationRow){
					if(destinationColumn == (originalColumn + 1) || destinationColumn == (originalColumn - 1)){
						return true;
					}
				}
				else if(originalColumn == destinationColumn){
					if(destinationRow == (originalRow + 1) || destinationRow == (originalRow - 1)){
						return true;
					}
				}
				else if(aShogiPiece.getOrientation().equalsIgnoreCase("left")){
					if((destinationColumn == (originalColumn + 1)) && (destinationRow == (originalRow - 1) || destinationRow ==(originalRow + 1))){
						return true;
					}
				}
				else if(aShogiPiece.getOrientation().equalsIgnoreCase("right")){
					if((destinationColumn == (originalColumn - 1)) && (destinationRow == (originalRow - 1) || destinationRow ==(originalRow + 1))){
						return true;
					}
				}				
				else
					return false;
			case 's':
				if((destinationRow == originalRow + 1 || destinationRow == originalRow - 1) && 
					(destinationColumn == originalColumn + 1 || destinationColumn == originalColumn - 1)){
					return true;
				}
				else if((aShogiPiece.getOrientation().equalsIgnoreCase("left") &&
				destinationIndex == (originalIndex + 1)) ||
				(aShogiPiece.getOrientation().equalsIgnoreCase("right") &&
				destinationIndex == (originalIndex - 1)))
					return true;
				else
					return false;	
			case 'b' :
				legalBishop = false;
				if(Math.abs(originalRow - destinationRow) == Math.abs(originalColumn - destinationColumn)){
					legalBishop = true;
					if(destinationRow < originalRow && destinationColumn < originalColumn){
						for(int delta = 1;(delta <= Math.abs(originalRow-destinationRow) && delta <= Math.abs(originalColumn-destinationColumn)); delta++){
							if(getRightPlacement(9*(originalRow-delta)+originalColumn-delta) == 1 || 
								getLeftPlacement(9 * (originalRow-delta)+originalColumn-delta) == 1){
								if(originalRow-delta == destinationRow){
									legalBishop = true;
								} else
									legalBishop = false;
								break;
							}
						}
					}
					else if(destinationRow > originalRow && destinationColumn > originalColumn){
						for(int delta = 1;(delta <= Math.abs(originalRow-destinationRow) && delta <= Math.abs(originalColumn-destinationColumn)); delta++){
							if(getRightPlacement(9*(originalRow+delta)+originalColumn+delta) == 1 || 
								getLeftPlacement(9*(originalRow+delta)+originalColumn+delta) == 1){
								if(originalRow+delta == destinationRow){
									legalBishop = true;
								} else
									legalBishop = false;
								break;
							}
						}
					}
					else if(destinationRow < originalRow && destinationColumn > originalColumn){
						for(int delta = 1;(delta <= Math.abs(originalRow-destinationRow) && delta <= Math.abs(originalColumn-destinationColumn)); delta++){
							if(getRightPlacement(9*(originalRow-delta)+originalColumn+delta) == 1 || 
								getLeftPlacement(9*(originalRow-delta)+originalColumn+delta) == 1){
								if(originalRow-delta == destinationRow){
									legalBishop = true;
								} else
									legalBishop = false;
								break;
							}
						}					
					}
					else if(destinationRow > originalRow && destinationColumn < originalColumn){
						for(int delta = 1;(delta <= Math.abs(originalRow-destinationRow) && delta <= Math.abs(originalColumn-destinationColumn)); delta++){
							if(getRightPlacement(9*(originalRow+delta)+originalColumn-delta) == 1 || 
								getLeftPlacement(9*(originalRow+delta)+originalColumn-delta) == 1) {
								if(originalRow+delta == destinationRow){
									legalBishop = true;
								} else
									legalBishop = false;
								break;
							}
						}					
					}
					return legalBishop;
				} else
					return false;
			case 'r':
				if(originalRow == destinationRow){
					if(destinationIndex > originalIndex){
						
						for(int i = originalIndex + 1; i < destinationIndex; i++){
							if(getRightPlacement(i) == 1 || getLeftPlacement(i) == 1){
									return false;
							}
						}
						return true;
					}

					if(destinationIndex < originalIndex){
						for(int i = originalIndex - 1; i > destinationIndex; i--){
							if(getRightPlacement(i) == 1 || getLeftPlacement(i) == 1){
									return false;
							}
						}
						return true;
					}
				}
				else if(destinationColumn == originalColumn){
					if(destinationIndex > originalIndex){
						
						for(int i = originalIndex + 9; i < destinationIndex; i += 9){
							if(getRightPlacement(i) == 1 || getLeftPlacement(i) == 1){
									return false;
							}
						}
						return true;
					}

					else if(destinationIndex < originalIndex){
						for(int i = originalIndex - 9; i > destinationIndex; i -= 9){
							if(getRightPlacement(i) == 1 || getLeftPlacement(i) == 1){
									return false;
							}
						}
						return true;
					}
				}				
				
				else
					return false;
			case 'p':
				if((aShogiPiece.getOrientation().equalsIgnoreCase("left") &&
				destinationIndex == (originalIndex + 1)) ||
				(aShogiPiece.getOrientation().equalsIgnoreCase("right") &&
				destinationIndex == (originalIndex - 1)))
					return true;
				else
					return false;
			case 'k':
				if(Math.abs(originalRow - destinationRow) < 2 &&
				Math.abs(originalColumn - destinationColumn) < 2)
					return true;
				else
					return false;
			case 'l':
				if(originalRow == destinationRow){
					if(aShogiPiece.getOrientation().equalsIgnoreCase("left") && originalColumn < destinationColumn){
						for(int i = originalIndex + 1; i < destinationIndex; i++){
							if(getRightPlacement(i) == 1 || getLeftPlacement(i) == 1){
									return false;
							}
						}
						return true;
					}
					if(aShogiPiece.getOrientation().equalsIgnoreCase("right") && originalColumn > destinationColumn){
						for(int i = originalIndex - 1; i > destinationIndex; i--){
							if(getRightPlacement(i) == 1 || getLeftPlacement(i) == 1){
									return false;
							}
						}
						return true;
					}
				}
				else
					return false;
			default:
				return false;
		}
	}
	
}
